﻿using UnityEngine;
using System.Collections;

[ExecuteInEditMode]
public class ParticleShaderBatchAdjustColor : MonoBehaviour
{
	private static int colorNameId = -1;
	private static int colorStrengthNameId = -1;

	public Color color = Color.white;

	public float colorStrength = 1.0f;

	public bool updateColorOnce = false;

	public Renderer[] renderers;

	public Material sharedMaterial;

	[System.NonSerialized]
	private Material _cloneMaterial;

#if UNITY_EDITOR
	void Reset()
	{
		if (sharedMaterial == null)
		{
			return;
		}
		if (_cloneMaterial == null)
		{
			_cloneMaterial = new Material (sharedMaterial);
			colorNameId = Shader.PropertyToID ("_TintColor");
			colorStrengthNameId = Shader.PropertyToID("_ColorStrength");
			color = sharedMaterial.GetColor (colorNameId);
			if (sharedMaterial.HasProperty (colorStrengthNameId))
			{
				colorStrength = sharedMaterial.GetFloat (colorStrengthNameId);
			}
		}

		int count = renderers != null ? renderers.Length : 0;
		for (int i = 0; i < count; ++i)
		{
			renderers [i].sharedMaterial = _cloneMaterial;
		}
	}
	#endif

	#if !UNITY_EDITOR
	void Start()
	{
		if (sharedMaterial == null)
		{
			return;
		}

		if (_cloneMaterial == null)
		{
			_cloneMaterial = new Material (sharedMaterial);
			colorNameId = Shader.PropertyToID ("_TintColor");
			colorStrengthNameId = Shader.PropertyToID("_ColorStrength");
			color = sharedMaterial.GetColor (colorNameId);
			if (sharedMaterial.HasProperty (colorStrengthNameId))
			{
				colorStrength = sharedMaterial.GetFloat (colorStrengthNameId);
			}
		}

		int count = renderers != null ? renderers.Length : 0;
		for (int i = 0; i < count; ++i)
		{
			renderers [i].sharedMaterial = _cloneMaterial;
			renderers [i].enabled = true;
		}
	}
	#endif

	void Awake()
	{
		int count = renderers != null ? renderers.Length : 0;
		for (int i = 0; i < count; ++i)
		{
			renderers [i].sharedMaterial = null;
			renderers [i].enabled = false;
		}
	}

	void Update()
	{
		if (sharedMaterial == null)
		{
			return;
		}
		#if UNITY_EDITOR
		if (_cloneMaterial == null)
		{
			_cloneMaterial = new Material (sharedMaterial);
			colorNameId = Shader.PropertyToID ("_TintColor");
			colorStrengthNameId = Shader.PropertyToID("_ColorStrength");
			color = sharedMaterial.GetColor (colorNameId);
			if (sharedMaterial.HasProperty (colorStrengthNameId))
			{
				colorStrength = sharedMaterial.GetFloat (colorStrengthNameId);
			}
		}

		int count = renderers != null ? renderers.Length : 0;
		for (int i = 0; i < count; ++i)
		{
			renderers [i].sharedMaterial = _cloneMaterial;
			renderers [i].enabled = true;
		}

		#endif
		if (_cloneMaterial != null)
		{
			_cloneMaterial.SetVector (colorNameId, color);
			_cloneMaterial.SetFloat (colorStrengthNameId, colorStrength);
		}

		#if !UNITY_EDITOR
		if (updateColorOnce)
		{
		this.enabled = false;
		}
		#endif
	}

	void OnDestroy()
	{
		if (_cloneMaterial != null)
		{
			int count = renderers != null ? renderers.Length : 0;
			for (int i = 0; i < count; ++i)
			{
				renderers [i].sharedMaterial = null;
			}
			if (Application.isPlaying)
			{
				Object.Destroy (_cloneMaterial);
			} else
			{
				Object.DestroyImmediate (_cloneMaterial);
			}
			_cloneMaterial = null;
		}
	}
}

